package com.dzx.currentency.deadlock;

/**
 * @author DuanZhaoXu
 * @ClassName:
 * @Description:
 * @date 2018年11月20日 17:38:30
 */
public class DeadLockTest implements  Runnable {

    private int flag ;

    private static Object o1=new Object();
    private static Object o2=new Object();

    public DeadLockTest(int flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        if(flag == 0){
            synchronized (o2){

                try {
                    Thread.sleep(500);
                    System.out.println("Thread1开始");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println("Thread1结束！");
                }
            }
        }
        if(flag ==1){
            synchronized (o1){
                try {
                    Thread.sleep(500);
                    System.out.println("Thread2开始");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println("Thread2结束！");
                }
            }
        }


    }
    public static void  main(String[] args){

        new Thread(new DeadLockTest(0)).start();
        new Thread(new DeadLockTest(1)).start();
    }

}
